﻿using DX;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Tools
{
    public interface IManifestFileService
    {
        T Deserialize<T>(Assembly assembly, string name);
        T Deserialize<T>(string name);
    }
    public class ManifestFileService : IManifestFileService
    {
        private readonly ISerializeProvider serializeProvider;

        public ManifestFileService(ISerializeProvider serializeProvider)
        {
            this.serializeProvider = serializeProvider;
        }
        public T Deserialize<T>(string name)
        {
            return Deserialize<T>(Assembly.GetEntryAssembly(), name);
        }

        public T Deserialize<T>(Assembly assembly, string name)
        {
            using var stream = assembly.GetManifestResourceStream(name);
            if (stream == null)
            {
                return default;
            }
            using var textReader = new StreamReader(stream);
            var json = textReader.ReadToEnd();
            var list = serializeProvider.Deserialize<T>(json);
            return list;
        }
    }
}
